<%@page import="com.nxu.entity.UserBean"%>
<%@page import="com.nxu.entity.BookBean"%>
<%@page import="java.util.List"%>
<%@page import="com.nxu.biz.impl.BooksBizImpl"%>
<%@page import="com.nxu.biz.BooksBiz"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%-- 权限检查：只有管理员(userType=1)可以访问 --%>
<%
    UserBean user = (UserBean)session.getAttribute("user");
    if(user == null || user.getUserType() != 1) {
        response.sendRedirect("login.jsp");
        return;
    }
%>

<%
// 分页参数处理
int pageSize = 10;
int currentPage = 1;

String pageParam = request.getParameter("page");
if(pageParam != null && !pageParam.isEmpty()) {
    currentPage = Integer.parseInt(pageParam);
}

BooksBiz biz = new BooksBizImpl();
int totalCount = biz.getTotalCount();
List<BookBean> books = biz.getBooksByPage(currentPage, pageSize);
int totalPages = (int) Math.ceil((double)totalCount / pageSize);
%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>书香阁 - 书籍管理系统</title>
    <style>
        :root {
            --primary-color: #8d6e63;
            --danger-color: #d32f2f;
            --success-color: #388e3c;
            --light-gray: #f5f5f5;
            --dark-gray: #5a3921;
            --text-color: #333;
            --border-color: #d7ccc8;
        }
        
        body {
            font-family: 'Arial', sans-serif;
            margin: 0;
            padding: 0;
            background: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)), 
                        url('https://images.unsplash.com/photo-1507842217343-583bb7270b66?ixlib=rb-1.2.1&auto=format&fit=crop&w=1350&q=80') no-repeat center center fixed;
            background-size: cover;
            color: var(--text-color);
        }
        
        .container {
            max-width: 1400px;
            margin: 20px auto;
            background: rgba(255, 255, 255, 0.95);
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 10px 25px rgba(0,0,0,0.2);
            backdrop-filter: blur(5px);
            animation: fadeIn 0.5s ease-in-out;
        }
        
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(-20px); }
            to { opacity: 1; transform: translateY(0); }
        }
        
        .user-info {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 1px solid var(--border-color);
        }
        
        .user-status {
            display: flex;
            align-items: center;
            gap: 10px;
        }
        
        .user-avatar {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            background-color: var(--primary-color);
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: bold;
        }
        
        .user-name {
            font-weight: bold;
            color: var(--dark-gray);
        }
        
        .logout-btn {
            padding: 8px 15px;
            background-color: var(--primary-color);
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: all 0.3s;
        }
        
        .logout-btn:hover {
            background-color: #6d4c41;
            transform: translateY(-2px);
        }
        
        h1 {
            text-align: center;
            color: var(--dark-gray);
            margin-bottom: 25px;
            padding-bottom: 15px;
            border-bottom: 2px solid var(--primary-color);
        }
        
        .action-buttons {
            display: flex;
            justify-content: space-between;
            margin-bottom: 20px;
        }
        
        .btn {
            padding: 8px 15px;
            border-radius: 4px;
            cursor: pointer;
            text-decoration: none;
            font-weight: 500;
            transition: all 0.2s;
            border: none;
            display: inline-flex;
            align-items: center;
            gap: 5px;
        }
        
        .btn-primary {
            background-color: var(--primary-color);
            color: white;
        }
        
        .btn-primary:hover {
            background-color: #6d4c41;
            transform: translateY(-1px);
        }
        
        .btn-danger {
            background-color: var(--danger-color);
            color: white;
        }
        
        .btn-edit {
            background-color: #f39c12;
            color: white;
        }
        
        .table-container {
            overflow-x: auto;
            margin: 20px 0;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        
        table {
            width: 100%;
            border-collapse: collapse;
            font-size: 14px;
        }
        
        th, td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid var(--border-color);
        }
        
        th {
            background-color: var(--light-gray);
            font-weight: 600;
            white-space: nowrap;
            color: var(--dark-gray);
        }
        
        tr:hover {
            background-color: rgba(141, 110, 99, 0.05);
        }
        
        .book-cover {
            width: 50px;
            height: 70px;
            object-fit: cover;
            border-radius: 3px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }
        
        .action-cell {
            white-space: nowrap;
        }
        
        .action-cell .btn {
            margin-right: 5px;
            padding: 6px 10px;
            font-size: 13px;
        }
        
        .action-cell .btn:last-child {
            margin-right: 0;
        }
        
        .pagination {
            display: flex;
            justify-content: center;
            margin-top: 25px;
            gap: 5px;
        }
        
        .pagination a {
            padding: 8px 12px;
            border: 1px solid var(--border-color);
            border-radius: 4px;
            text-decoration: none;
            color: var(--text-color);
        }
        
        .pagination a:hover {
            background-color: #f0f0f0;
        }
        
        .pagination .active {
            background-color: var(--primary-color);
            color: white;
            border-color: var(--primary-color);
        }
        
        .page-info {
            text-align: center;
            margin: 15px 0;
            color: var(--dark-gray);
            font-weight: 500;
        }
        
        /* 模态框样式 */
        .modal {
            display: none;
            position: fixed;
            z-index: 100;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0,0,0,0.5);
            overflow: auto;
        }
        
        .modal-content {
            background-color: white;
            margin: 50px auto;
            padding: 25px;
            border-radius: 8px;
            width: 50%;
            max-width: 600px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.2);
            position: relative;
        }
        
        .close {
            position: absolute;
            top: 15px;
            right: 20px;
            font-size: 24px;
            color: #aaa;
            cursor: pointer;
        }
        
        .close:hover {
            color: #333;
        }
        
        .form-group {
            margin-bottom: 15px;
        }
        
        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: 500;
            color: var(--dark-gray);
        }
        
        .form-group input, 
        .form-group select {
            width: 100%;
            padding: 8px;
            border: 1px solid var(--border-color);
            border-radius: 4px;
            box-sizing: border-box;
        }
        
        .form-actions {
            display: flex;
            justify-content: flex-end;
            gap: 10px;
            margin-top: 20px;
        }
        
        @media (max-width: 768px) {
            .modal-content {
                width: 90%;
                margin: 30px auto;
            }
            
            .container {
                padding: 15px;
            }
            
            th, td {
                padding: 8px 10px;
            }
            
            .action-buttons {
                flex-direction: column;
                gap: 10px;
            }
        }
    </style>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
</head>
<body>
    <div class="container">
        <div class="user-info">
            <div class="user-status">
                <div class="user-avatar"><%= user.getUsername().substring(0, 1).toUpperCase() %></div>
                <span class="user-name">管理员：<%= user.getUsername() %></span>
            </div>
            <button class="logout-btn" onclick="logout()">
                <i class="fas fa-sign-out-alt"></i> 退出登录
            </button>
        </div>
        
        <h1><i class="fas fa-book-open"></i> 书香阁 - 书籍管理系统</h1>
        
        <div class="action-buttons">
            <div class="page-info">
                共 <%= totalCount %> 本书 | 第 <%= currentPage %> 页/共 <%= totalPages %> 页
            </div>
            <button class="btn btn-primary" onclick="openAddModal()">
                <i class="fas fa-plus"></i> 添加新书
            </button>
        </div>
        
        <div class="table-container">
            <table>
                <thead>
                    <tr>
                        <th>封面</th>
                        <th>ISBN</th>
                        <th>书名</th>
                        <th>版权年份</th>
                        <th>版次</th>
                        <th>出版社ID</th>
                        <th>价格</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <% if(books.isEmpty()) { %>
                    <tr>
                        <td colspan="8" style="text-align: center; padding: 30px;">
                            <i class="fas fa-book" style="font-size: 24px; color: #ccc;"></i><br>
                            暂无书籍数据
                        </td>
                    </tr>
                    <% } else { 
                        for (BookBean book : books) {
                    %>
                    <tr>
                        <td>
                            <img src="images/<%= book.getImagefile() %>" 
                                 alt="<%= book.getTitle() %>" 
                                 class="book-cover"
                                 onerror="this.src='images/default-book.jpg'">
                        </td>
                        <td><%= book.getIsbn() %></td>
                        <td><%= book.getTitle() %></td>
                        <td><%= book.getCopyright() %></td>
                        <td><%= book.getEditionnumber() %></td>
                        <td><%= book.getPublisherid() %></td>
                        <td>￥<%= book.getPrice() %></td>
                        <td class="action-cell">
                            <button class="btn btn-edit" onclick="openEditModal('<%= book.getIsbn() %>')">
                                <i class="fas fa-edit"></i> 编辑
                            </button>
                            <a href="deleteBook.jsp?isbn=<%= book.getIsbn() %>" 
                               class="btn btn-danger" 
                               onclick="return confirm('确定要删除《<%= book.getTitle() %>》吗？')">
                                <i class="fas fa-trash-alt"></i> 删除
                            </a>
                        </td>
                    </tr>
                    <% } } %>
                </tbody>
            </table>
        </div>
        
        <div class="pagination">
            <% if(currentPage > 1) { %>
                <a href="manage.jsp?page=1"><i class="fas fa-angle-double-left"></i></a>
                <a href="manage.jsp?page=<%= currentPage-1 %>"><i class="fas fa-angle-left"></i></a>
            <% } %>
            
            <a class="active"><%= currentPage %></a>
            
            <% if(currentPage < totalPages) { %>
                <a href="manage.jsp?page=<%= currentPage+1 %>"><i class="fas fa-angle-right"></i></a>
                <a href="manage.jsp?page=<%= totalPages %>"><i class="fas fa-angle-double-right"></i></a>
            <% } %>
        </div>
    </div>

    <!-- 添加书籍模态框 -->
    <div id="addModal" class="modal">
        <div class="modal-content">
            <span class="close" onclick="closeAddModal()">&times;</span>
            <h2><i class="fas fa-plus-circle"></i> 添加新书</h2>
            <form action="addBook.jsp" method="post" enctype="multipart/form-data">
                <div class="form-group">
                    <label for="isbn">ISBN:</label>
                    <input type="text" id="isbn" name="isbn" required>
                </div>
                <div class="form-group">
                    <label for="title">书名:</label>
                    <input type="text" id="title" name="title" required>
                </div>
                <div class="form-group">
                    <label for="copyright">版权年份:</label>
                    <input type="text" id="copyright" name="copyright" maxlength="4" required>
                </div>
                <div class="form-group">
                    <label for="editionnumber">版次:</label>
                    <input type="number" id="editionnumber" name="editionnumber" min="1" required>
                </div>
                <div class="form-group">
                    <label for="publisherid">出版社ID:</label>
                    <input type="number" id="publisherid" name="publisherid" min="1" required>
                </div>
                <div class="form-group">
                    <label for="price">价格:</label>
                    <input type="number" id="price" name="price" min="0" step="1" required>
                </div>
                <div class="form-group">
                    <label for="imagefile">封面图片:</label>
                    <input type="file" id="imagefile" name="imagefile" accept="image/*">
                </div>
                <div class="form-actions">
                    <button type="button" class="btn btn-danger" onclick="closeAddModal()">
                        <i class="fas fa-times"></i> 取消
                    </button>
                    <button type="submit" class="btn btn-primary">
                        <i class="fas fa-save"></i> 保存
                    </button>
                </div>
            </form>
        </div>
    </div>

    <!-- 编辑书籍模态框 -->
    <div id="editModal" class="modal">
        <div class="modal-content">
            <span class="close" onclick="closeEditModal()">&times;</span>
            <h2><i class="fas fa-edit"></i> 编辑书籍</h2>
            <form action="updateBook.jsp" method="post" enctype="multipart/form-data">
                <input type="hidden" id="edit-isbn" name="isbn">
                <div class="form-group">
                    <label for="edit-title">书名:</label>
                    <input type="text" id="edit-title" name="title" required>
                </div>
                <div class="form-group">
                    <label for="edit-copyright">版权年份:</label>
                    <input type="text" id="edit-copyright" name="copyright" maxlength="4" required>
                </div>
                <div class="form-group">
                    <label for="edit-editionnumber">版次:</label>
                    <input type="number" id="edit-editionnumber" name="editionnumber" min="1" required>
                </div>
                <div class="form-group">
                    <label for="edit-publisherid">出版社ID:</label>
                    <input type="number" id="edit-publisherid" name="publisherid" min="1" required>
                </div>
                <div class="form-group">
                    <label for="edit-price">价格:</label>
                    <input type="number" id="edit-price" name="price" min="0" step="1" required>
                </div>
                <div class="form-group">
                    <label for="edit-imagefile">封面图片:</label>
                    <input type="file" id="edit-imagefile" name="imagefile" accept="image/*">
                    <small style="color: #666;">留空则不修改图片</small>
                </div>
                <div class="form-actions">
                    <button type="button" class="btn btn-danger" onclick="closeEditModal()">
                        <i class="fas fa-times"></i> 取消
                    </button>
                    <button type="submit" class="btn btn-primary">
                        <i class="fas fa-save"></i> 更新
                    </button>
                </div>
            </form>
        </div>
    </div>

    <script>
        // 打开添加模态框
        function openAddModal() {
            document.getElementById('addModal').style.display = 'block';
        }
        
        // 关闭添加模态框
        function closeAddModal() {
            document.getElementById('addModal').style.display = 'none';
        }
        
        // 打开编辑模态框
        function openEditModal(isbn) {
            fetch('getBookInfo.jsp?isbn=' + isbn)
                .then(response => response.json())
                .then(book => {
                    document.getElementById('edit-isbn').value = book.isbn;
                    document.getElementById('edit-title').value = book.title;
                    document.getElementById('edit-copyright').value = book.copyright;
                    document.getElementById('edit-editionnumber').value = book.editionnumber;
                    document.getElementById('edit-publisherid').value = book.publisherid;
                    document.getElementById('edit-price').value = book.price;
                    document.getElementById('editModal').style.display = 'block';
                })
                .catch(error => {
                    console.error('Error:', error);
                    alert('获取书籍信息失败');
                });
        }
        
        // 关闭编辑模态框
        function closeEditModal() {
            document.getElementById('editModal').style.display = 'none';
        }
        
        // 点击模态框外部关闭模态框
        window.onclick = function(event) {
            if (event.target.className === 'modal') {
                closeAddModal();
                closeEditModal();
            }
        }
        
        // 退出登录
        function logout() {
            if(confirm('确定要退出登录吗？')) {
                window.location.href = 'login.jsp';
            }
        }
    </script>
</body>
</html>